<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');

define('MAGENTO', dirname(dirname(__FILE__)));
require_once(MAGENTO . '/app/Mage.php');
Mage::app('default', 'store');

function saveCart(Mage_Checkout_Model_Cart $cart)
{
    Mage::dispatchEvent('checkout_cart_save_before', array('cart' => $cart));

    $cart->getQuote()->getBillingAddress();
    $cart->getQuote()->getShippingAddress()->setCollectShippingRates(false);
    $cart->getQuote()->collectTotals();
    $cart->getQuote()->save();
    $cart->getCheckoutSession()->setQuoteId($cart->getQuote()->getId());
    /**
     * Cart save usually called after changes with cart items.
     */
    Mage::dispatchEvent('checkout_cart_save_after', array('cart' => $cart));
}

function createOrder()
{
    $customerEmail = '53254535@qq.com';

    try {
        /** @var Mage_Customer_Model_Customer $customer */
        $customer = Mage::getModel('customer/customer');
        $customer->setData('website_id', Mage::app()->getWebsite()->getId());
        $customer->loadByEmail($customerEmail);

        /** @var Mage_Customer_Model_Session $customerSession */
        $customerSession = Mage::getSingleton('customer/session');
        $customerSession->setCustomer($customer);
    } catch (Exception $ex) {
        echo $ex->getMessage() . PHP_EOL;
        echo $ex->getTraceAsString() . PHP_EOL;
        exit;
    }

    $address = array(
        'cn_name' => '汪飞',
        'street' => array(
            '0' => '天钥桥路386号2楼',
        ),
        'cn_province' => '9',
        'cn_district' => '722',
        'postcode' => '200000',
        'country_id' => 'CN',
        'telephone' => '021-33566011',
    );

    /** @var Mage_Checkout_Model_Cart $cart */
    $cart = Mage::getSingleton('checkout/cart');
    $cart->truncate();

    try {
        $cart->addProduct(329, array(
            'bundle_option' => array(
                14 => 17,
                15 => 18
            )
        ));
        saveCart($cart);
    } catch (Exception $ex) {
        echo $ex->getMessage() . PHP_EOL;
        echo $ex->getTraceAsString() . PHP_EOL;
        exit;
    }

    try {
        $quote = $cart->getQuote();
        $quote->setCustomer($customer);

        /** @var Mage_Sales_Model_Quote_Address $quoteAddress */
        $quoteAddress = Mage::getModel('sales/quote_address');
        $quoteAddress->addData($address);

        $quote->setShippingAddress($quoteAddress);
        $quote->getShippingAddress()->setShippingMethod('tablerate');
        $quote->getPayment()->importData(array('method' => 'easipayb2c_payment'));

        $quote->setIsActive(1);
        $quote->collectTotals();

        /** @var Hln35_CnCheckout_Model_Service_Quote $service */
        $service = Mage::getModel('sales/service_quote', $cart->getQuote());
        $service->submitAll();
    } catch (Exception $ex) {
        echo $ex->getMessage() . PHP_EOL;
        echo $ex->getTraceAsString() . PHP_EOL;
        exit;
    }
}

function createOrders($count)
{
    for ($i = 0; $i < $count; $i++) {
        createOrder();
    }
}

createOrders(15);